<?php namespace Admin\Model;
use Hdphp\Model\Model;
class Goods_attr extends Model{
    protected $table="goods_attr";


    //通过gid获得筛选属性
public function getGoodsAttr($gids){
    if($gids){
            //获取这些同类商品的所有属性
        $goodsAttr = $this->where("goods_gid in(".implode(',',$gids).")")->groupBy('gavalue')->get();
            //把相同的类型归结在一起
        $temp1 = array();
        foreach($goodsAttr as $v){
            $temp1[$v['type_attribute_taid']][] = $v;
        }
        $temp2 = array();
        foreach($temp1 as $taid=>$value){
            $name = Db::table('type_attribute')->where("taid={$taid}")->pluck('taname');
            $temp2[] = array(
                'name' => $name,
                'value' => $value
            );
        }
        return $temp2;
    }else{
        return array();
    }
}
    //通过商品属性id进行筛选商品
public function filterGid($param,$cidGids){
    $gids = array();
    foreach($param as $gaid){
        //如果不是"全部"的时候
        if($gaid){
            //通过taid获得属性的名称
            $name = $this->where("gaid = {$gaid}")->pluck("gavalue");
            //通过属性名称查找商品gid
            $gids[] = $this->where("gavalue = '{$name}'")->lists('goods_gid');
        }
    }
        //如果有点击任何一个筛选属性的时候
    if($gids){
        //把获得好的商品gid取交集
        $gidsOne = $gids[0];
        foreach($gids as $v){
            $gidsOne = array_intersect($v, $gidsOne);
        }
        //再和分类查找出来的$cidGids取交集
        $finalGids = array_intersect($gidsOne, $cidGids);
        return $finalGids;
    }else{ //如果全部点击全部的时候
        return $cidGids;
    }







}



}
?>